package com.example.demo.utils;


import org.springframework.http.HttpStatus;

import java.io.Serializable;

/**
 *
 * @author c10709
 * @date 2018/7/31
 */
public class ResponseData<T> implements Serializable {
	/**
	 * 返回客户端统一格式，包括状态码，提示信息，以及业务数据
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * 返回成功标识,便于前端判断
	 */
	private Boolean status;
	/**
	 * 必要的提示信息
	 */
	private String message;
	/**
	 * 业务数据
	 */
	private T data;

	/** 请求获取的状态码，类似HttpCode */
	private Integer code;

//    /** 请求获取的数据（map类型） */
//    private Map<String, Object> attributes;

    private ResponseData(Boolean status, Integer code) {
		this.status = status;
		this.code = code;
	}

	private ResponseData(Boolean status, Integer code, T data) {
		this.status = status;
		this.code = code;
		this.data = data;
	}

	private ResponseData(Boolean status, Integer code, String message, T data) {
		this.status = status;
		this.code = code;
		this.message = message;
		this.data = data;
	}

	private ResponseData(Boolean status, Integer code, String message) {
		this.status = status;
		this.code = code;
		this.message = message;
	}

//    private ResponseData(Boolean status, Integer code, T data, Map<String, Object> attributes, String message) {
//        this.status = status;
//        this.code = code;
//        this.data = data;
//        this.attributes = attributes;
//        this.message = message;
//    }

	/**
	 * 返回成功
	 * 
	 * @return
	 */
	public static <T> ResponseData<T> success() {
		return new ResponseData<>(true, HttpStatus.OK.value());
	}

	/**
	 */
	public static <T> ResponseData<T> success(String message) {
		return new ResponseData<>(true, HttpStatus.OK.value(), message);
	}

	public static <T> ResponseData<T> success(Integer code, String message) {
		return new ResponseData<>(true, code, message);
	}

	/**
	 * 返回成功，并返回数据
	 * 
	 * @param data
	 * @param
	 * @return
	 */
	public static <T> ResponseData<T> success(T data) {
		return new ResponseData<>(true, HttpStatus.OK.value(), data);
	}

//    public static <T> ResponseData<T> success(T data, Map<String, Object> attributes, String message) {
//        return new ResponseData<>(true, HttpStatus.OK.value(), data, attributes, message);
//    }

    public static <T> ResponseData<T> success(String message, T data) {
        return new ResponseData<>(true, HttpStatus.OK.value(), message, data);
    }

	/**
	 * 返回错误
	 * 
	 * @return
	 */
	public static <T> ResponseData<T> failure() {
		return new ResponseData<>(false, HttpStatus.NOT_FOUND.value());
	}

	/**
	 * 返回错误
	 * 
	 * @return
	 */
	public static <T> ResponseData<T> failure(String message) {
		return new ResponseData<>(false, HttpStatus.NOT_FOUND.value(),message);
	}

	/**
	 * 返回错误的消息
	 * 
	 * @param message
	 * @return
	 */
	public static <T> ResponseData<T> failure(Integer code, String message) {
		return new ResponseData<>(false, code, message);
	}

	public Integer getCode() {
		return code;
	}

	public Boolean getStatus() {
		return status;
	}

	public String getMessage() {
		return message;
	}

	public T getData() {
		return data;
	}

//    public Map<String, Object> getAttributes() {
//        return attributes;
//    }
//
//    public void setAttributes(Map<String, Object> attributes) {
//        this.attributes = attributes;
//    }

}
